<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	if($_SESSION['uid']){
		$member=$this->model('member')->get_member();
	}else{
		$return['ischecklogin']=$this->model('member')->get_member();
	}
	$this->model('member')->checkmyinfo();
	$_SESSION['forward_url']='jy_xiangqin/member.index/';
	$_SESSION['indexurl']='jy_xiangqin/index/';
	$return['item']=model('core/member')->detail($_SESSION['uid']);
	$credit_types=credit_types();
	$pay=uni_setting('pay');
	foreach($credit_types as $k=>$cv){
		if($k>2){
			$prizeType[]=array(
				'id'=>$k,
				'title'=>$cv,
				'api'=>$pay['credit'.$k.'_api'],
				'data'=>$return['item']['credit'.$k],
			);
		}
	}
	$return['item']['credit_type']=$prizeType;
	$member['level']=pdo_get('jy_xiangqin_level',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['level_id']));
	$member['level']=$member['level']?:array('title'=>'普通会员','level'=>0);
	$member['nextlevel']=pdo_get('jy_xiangqin_level',array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'level >'=>$member['level']['level']),'','level asc');
	$_SESSION['sex']=$member['sex'];
	$member['nickname']=$member['nickname']?:$return['item']['nickname'];
	$member['avatar']=$member['avatar']?:$return['item']['avatar'];
	$return['item']['avatar']=$member['avatar']?:$return['item']['avatar'];
	$member['images']=$member['images']?json_decode($member['images'],true):array();
	$member['birth']=json_decode($member['birth'],true);
	foreach($member['images'] as &$img){
		$img=tomedia($img);
	}
	$member['avatar']=$member['avatar']?:$item['images'][0];
	$member['siimgs']=$member['siimgs']?json_decode($member['siimgs'],true):array();
	foreach($member['siimgs'] as &$img){
		$img=tomedia($img);
	}
	$member['sextitle']=$this->sex[$member['sex']];
	$member['picsnum']=count($member['images']);
	$member['sipicsnum']=count($member['siimgs']);
	$current_year=date('Y');
	$member['age'] = $member['birth']['year']?$current_year - $member['birth']['year'] - 1:'**';
	$member['percent']=$this->model('member')->getpercent($member);
	$member['newsnum'] = pdo_count('jy_xiangqin_newslog',array('simi'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member['simivideo']=pdo_count('jy_xiangqin_newslog',array('simi'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	$member['fuwu'] = pdo_get('jy_xiangqin_fuwu',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['fuwu_id']));
	$member['worker'] = pdo_get('jy_xiangqin_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['worker_id']));
	$canyu=pdo_get('jy_xiangqin_pingdatilog',array('issub'=>1,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'','score desc');
	$member['maxscore']=$canyu['score'];
	$return['member']=$member;
	$tomember=$this->model('member')->get_tomember();
	$tomember['pcd']=json_decode($tomember['pcd'],true);
	$tomember['birth']=json_decode($tomember['birth'],true);
	$tomember['age'] = $tomember['birth']['year']?$current_year - $tomember['birth']['year'] - 1:'**';
	$return['tomember']=$tomember;
	$return['params']['type']=$_RQ['type']?:2;
	$status0 = pdo_count('jy_xiangqin_pingorderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>0));
	$status1 = pdo_count('jy_xiangqin_pingorderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>1));
	$status2 = pdo_count('jy_xiangqin_pingorderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>2));
	$status3 = pdo_count('jy_xiangqin_pingorderlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'status'=>3));
	$return['statics']=array(
		'status0'=>$status0,
		'status1'=>$status1,
		'status2'=>$status2,
		'status3'=>$status3,
	);
	$return['post']=array('type'=>3);	
	$poster=$this->plugin_setting('poster');
	$return['isposter']=$poster['isopen'];	
	if($_RQ['fuwuid']){
		$this->model('member')->upfuwuid($_SESSION['uid'],$_RQ['fuwuid']);
	}
	set_html('title','会员中心');
	exi($return);
}
if($_RQ['op']=='post'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$post=get_postdata();
	if(!$post['fee']){
		exi('请输入转账金额！','erroe');
	}
	if(!$post['mobile']){
		exi('请输入收款人账户/手机号！','erroe');
	}
	if(!$post['password']){
		exi('请输入支付密码！','erroe');
	}
	if($post['password']!=$member['paypassword']){
		exi('支付密码不正确！','erroe');
	}
	if($member['credit2']<$post['fee']){
		exi('转账额度不能大于，当前余额！','erroe');
	}
	$tomember=pdo_get('core_members',array('username'=>$post['mobile']));
	$tomid=$tomember['id'];
	if(!$tomid){
		$tomember=pdo_get('core_members',array('mobile'=>$post['mobile']));
		$tomid=$tomember['onemid']?:$tomember['id'];
	}
	if(!$tomid){
		exi('没找到收款人，转账失败！','erroe');
	}
	model('core/member')->cut_credit2($_SESSION['uid'],$post['fee'],'转账给:'.$post['mobile']);
	model('core/member')->add_credit2($tomid,$post['fee'],$member['username'].'的转账');
	exi('转账成功！');
}
if($_RQ['op']=='password'){
	$post=get_postdata();
	if(!$post['password']){
		exi('请输入支付密码！','erroe');
	}
	pdo_update('core_members',array('paypassword'=>$post['password']),array('id'=>$_SESSION['uid']));
	exi('设置成功！');
}
if($_RQ['op']=='upposter'){
	$openid=$_RQ['openid']?:$_SESSION['openid'];
	$posterset=$this->plugin_setting('poster');
	$basmember=model('core/member')->detail($_SESSION['uid'],array('credit1','credit2','mobile','avatar','nickname'));
	$member=$this->model('member')->get_member();
	$member['nickname']=$member['nickname']?:$basmember['nickname'];
	$member['avatar']=$member['avatar']?:$basmember['avatar'];
	$url=custom_url('jy_xiangqin/memberitem//fromuser='.$_SESSION['uid'],true);
	$qr=cfc('tools')->createqrcode($url,'250',true);
	$qr=tomedia($qr);
	$image='oss/'.$_SESSION['uniacid'].'/images/jy_xiangqin/wxappmid'.$_SESSION['uid'].'.jpg';
	$isadd=cfc('files')->source_check($image);
	if(!$isadd && $_SESSION['wxapp_id']){
			$wxappurl='/jy_xiangqin/memberitem/index?fromuser='.$_SESSION['uid'];
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$res=model('core/wxapp')->appcode($wxappurl,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_xiangqin/';
			if(!file_exists(ROOT_AT.$path)){
					mkdir(ROOT_AT.$path,0700, true);
			}
			$filename=$path.'/wxappmid'.$_SESSION['uid'].'.jpg';
			$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$wxappurl);
	}
	$return=plugin('jy_poster')->get_active('','','',$openid,$qr,$posterset['data'],'jy_xiangqin');
	$return['posterimg']=$return['poster'];
	exi($return);
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['jifen'])<=0){
		exi('请先设置额度！并且为整数！','error');
	}
	$this->setting['jifen_rate']=$this->setting['jifen_rate']?:1;
	$jifen_rate=round($this->setting['jifen_rate'],2)*1;
	$fee=$post['jifen']/$jifen_rate;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'credit'=>$post['jifen'],
		'type'=>$post['type'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'金币充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'core/member.xunibi/pay_result',
		'success_url'=>'core/member.xunibi//type='.$post['type'],
		'error_url'=>'core/member.xunibi//type='.$post['type'],
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	exi($pay_id);
}
